Page({
  data:{
    tabList: [
      { name: '酒店', image: '/assets/index/酒店.png', url: '/function/hotel/hotel' },
      { name: '机票', image: '/assets/index/国际机票.png', url: '/function/plane/plane' },
      { name: '火车票', image: '/assets/index/火车票.png', url: '/function/play/play' },
      { name: '旅游', image: '/assets/index/旅游.png', url: '/function/train/train' }
    ],
    activeTab: 0, // 当前选中的标签
    imageUrls: [], // 轮播图图片
    leftItems: [], // 左侧数据
    rightItems: [], // 右侧数据
    page: 1, // 当前页码
    pageSize: 3, // 每页数据量
    isRefreshing: false, // 是否正在刷新
    isLoadingMore: false, // 是否正在加载更多
  },

  onLoad() {
    this.loadImages();
    this.loadData();
  },

  // 加载轮播图图片
  loadImages() {
    // 模拟从服务器获取图片URL
    setTimeout(() => {
      const imageUrls = [
        'https://img1.qunarzz.com/travel/poi/1808/b4/b2b51ea3aed42a37.jpg',
        'https://pic1.zhimg.com/v2-0cc7136788db7ce363ef8c76d2d7be28_r.jpg',
        'https://th.bing.com/th/id/R.052a8bc533e6f5cbfea9354561e32bf1?rik=azM%2bwPpPauar5g&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50042%2f9675.jpg_wh1200.jpg&ehk=Vv0qcKv366byPGKs5%2beC8KaVbnhfYsaBbStjap9UV9E%3d&risl=&pid=ImgRaw&r=0',
      ];
      this.setData({ imageUrls });
    }, 1000);
  },

  // 切换订单状态标签
  onLoad() {
    this.loadImages();
    this.loadData();
    const activeTab = wx.getStorageSync('activeTab');
    if (activeTab !== undefined) {
      this.setData({
        activeTab: activeTab
      });
    }
  },

  // 页面显示时，再次尝试恢复激活的标签索引
  onShow() {
    const activeTab = wx.getStorageSync('activeTab');
    if (activeTab !== undefined) {
      this.setData({
        activeTab: activeTab
      });
    }
  },

  // 点击标签跳转到对应页面
  goToPage(e) {
    const url = e.currentTarget.dataset.url;
    const index = e.currentTarget.dataset.index;

    // 保存当前激活的标签索引到本地存储
    wx.setStorageSync('activeTab', index);

    // 使用 wx.navigateTo 或 wx.switchTab 跳转到对应页面
    wx.navigateTo({
      url: url
    });
  },

  // 加载数据
 // 加载数据
loadData() {
  const { page, pageSize } = this.data;
  const offset = (page - 1) * pageSize;

  // 请求左侧数据
  wx.request({
    url: 'https://7696-218-88-164-24.ngrok-free.app/images',
    method: 'GET',
    header: {
      'ngrok-skip-browser-warning': 'true'
    },
    data: {
      page,
      pageSize,
    },
    success: (res) => {
      // 确保 items 是一个数组
      const items = res.data.items || [];
      this.setData({
        leftItems: page === 1 ? items : [...this.data.leftItems, ...items],
      });
    },
    fail: (err) => {
      console.error('请求左侧数据失败', err);
      // 如果请求失败，确保 leftItems 不被设置为 undefined
      this.setData({
        leftItems: [],
      });
    },
  });

  // 请求右侧数据
  wx.request({
    url: 'https://7696-218-88-164-24.ngrok-free.app/user',
    method: 'GET',
    header: {
      'ngrok-skip-browser-warning': 'true'
    },
    data: {
      page,
      pageSize,
    },
    success: (res) => {
      // 确保 items 是一个数组
      const items = res.data.items || [];
      this.setData({
        rightItems: page === 1 ? items : [...this.data.rightItems, ...items],
      });
    },
    fail: (err) => {
      console.error('请求右侧数据失败', err);
      // 如果请求失败，确保 rightItems 不被设置为 undefined
      this.setData({
        rightItems: [],
      });
    },
    complete: () => {
      this.setData({
        isRefreshing: false,
        isLoadingMore: false,
      });
    },
  });
}
});